package org.example.day;

import java.util.Arrays;

/**
 * @Description: Solution3153
 * @Author wyatt
 * @Data 2024/08/30 15:25
 */
public class Solution3153 {

    public static void main(String[] args) {
        System.out.println(new Solution3153().sumDigitDifferences2(new int[]{13, 23, 12}));
    }

    public long sumDigitDifferences2(int[] nums) {
        long sum = 0;
        long n = nums.length;
        int num0 = nums[0];
        int[] counts = new int[10];
        while (num0 > 0){
            long subSum = 0;
            for (int i = 0; i < nums.length; i++) {
                counts[nums[i]%10]++;
                nums[i] = nums[i] / 10;
            }
            for (int j = 0; j < counts.length; j++) {
                subSum += (counts[j] * (n - counts[j]));
            }
            num0 = num0 / 10;
            Arrays.fill(counts, 0);
            sum+=subSum/2;
        }
        return sum;
    }

    public long sumDigitDifferences(int[] nums) {
        long sum = 0;
        for (int i = 0; i < nums.length; i++) {
            for (int j = i+1; j < nums.length; j++) {
                sum += sumDigitDif(nums[i], nums[j]);
            }
        }
        return sum;
    }

    private long sumDigitDif(int num1, int num2) {
        if(num1 == num2){
            return 0;
        }
        int sum = 0;
        while (num1 > 0){
            if(num1 % 10 != num2 %10){
                sum++;
            }
            num1 = num1 / 10;
            num2 = num2 / 10;
        }
        return sum;
    }

}
